<?php

namespace App\Services;

class DataMaskingService
{
    /**
     * 脱敏手机号
     *
     * @param string|null $phone
     * @return string|null
     */
    public function maskPhone(?string $phone): ?string
    {
        if (empty($phone)) {
            return $phone;
        }

        if (strlen($phone) === 11) {
            return substr($phone, 0, 3) . '****' . substr($phone, -4);
        }

        return $phone;
    }

    /**
     * 脱敏邮箱
     *
     * @param string|null $email
     * @return string|null
     */
    public function maskEmail(?string $email): ?string
    {
        if (empty($email)) {
            return $email;
        }

        $parts = explode('@', $email);
        if (count($parts) !== 2) {
            return $email;
        }

        $username = $parts[0];
        $domain = $parts[1];

        if (strlen($username) <= 2) {
            $maskedUsername = substr($username, 0, 1) . '*';
        } else {
            $maskedUsername = substr($username, 0, 2) . str_repeat('*', min(4, strlen($username) - 2)) . substr($username, -1);
        }

        return $maskedUsername . '@' . $domain;
    }

    /**
     * 脱敏身份证号
     *
     * @param string|null $idCard
     * @return string|null
     */
    public function maskIdCard(?string $idCard): ?string
    {
        if (empty($idCard)) {
            return $idCard;
        }

        $length = strlen($idCard);
        if ($length === 18) {
            return substr($idCard, 0, 6) . '********' . substr($idCard, -4);
        } elseif ($length === 15) {
            return substr($idCard, 0, 6) . '******' . substr($idCard, -3);
        }

        return $idCard;
    }

    /**
     * 脱敏银行卡号
     *
     * @param string|null $cardNumber
     * @return string|null
     */
    public function maskBankCard(?string $cardNumber): ?string
    {
        if (empty($cardNumber)) {
            return $cardNumber;
        }

        $length = strlen($cardNumber);
        if ($length >= 8) {
            return substr($cardNumber, 0, 4) . str_repeat('*', $length - 8) . substr($cardNumber, -4);
        }

        return $cardNumber;
    }

    /**
     * 脱敏姓名
     *
     * @param string|null $name
     * @return string|null
     */
    public function maskName(?string $name): ?string
    {
        if (empty($name)) {
            return $name;
        }

        $length = mb_strlen($name, 'UTF-8');
        if ($length === 1) {
            return $name;
        } elseif ($length === 2) {
            return mb_substr($name, 0, 1, 'UTF-8') . '*';
        } else {
            return mb_substr($name, 0, 1, 'UTF-8') . str_repeat('*', $length - 2) . mb_substr($name, -1, 1, 'UTF-8');
        }
    }

    /**
     * 脱敏地址
     *
     * @param string|null $address
     * @param int $keepLength 保留的字符长度
     * @return string|null
     */
    public function maskAddress(?string $address, int $keepLength = 6): ?string
    {
        if (empty($address)) {
            return $address;
        }

        $length = mb_strlen($address, 'UTF-8');
        if ($length <= $keepLength) {
            return $address;
        }

        return mb_substr($address, 0, $keepLength, 'UTF-8') . str_repeat('*', min(10, $length - $keepLength));
    }

    /**
     * 通用脱敏方法
     *
     * @param string|null $value
     * @param int $prefixLength 前缀保留长度
     * @param int $suffixLength 后缀保留长度
     * @return string|null
     */
    public function maskString(?string $value, int $prefixLength = 3, int $suffixLength = 4): ?string
    {
        if (empty($value)) {
            return $value;
        }

        $length = strlen($value);
        if ($length <= $prefixLength + $suffixLength) {
            return str_repeat('*', $length);
        }

        return substr($value, 0, $prefixLength) . str_repeat('*', $length - $prefixLength - $suffixLength) . substr($value, -$suffixLength);
    }
}

